package demo1230.lianxi;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

/*
客户端

public void shutdownOutput()throws IOException
输出流结束标志
 */
public class Socket_01 {
    public static void main(String[] args){
        //创建客户端对象
        try (Socket socket = new Socket("127.0.0.1", 9999)){
//            获取网络字节输出流中
            OutputStream os = socket.getOutputStream();
//       创建本地字节输入流对象,用来读取本地的文件
            FileInputStream fis = new FileInputStream("socket\\lyf.jpg");
            int read;
            byte[] bs = new byte[1024];
            while ((read = fis.read(bs)) != -1) {
                os.write(bs, 0, read);
            }
//            需要给结束信号
            socket.shutdownOutput();
            //获取网络字节输入流对象,读取服务器端回写的数据
            InputStream is = socket.getInputStream();
//           读取服务器回写的数据
            while ((read = is.read(bs)) != -1) {
                System.out.println(new String(bs, 0, read));
            }
            fis.close();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
